<?php

namespace app\models\searchs;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\models\Land;

/**
 * LandSearch represents the model behind the search form about `app\models\Land`.
 */
class LandSearch extends Land
{
    public $pname;
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'park_id', 'pricipal_id'], 'integer'],
            [['code', 'name', 'introduction', 'scale', 'map_image', 'tel', 'email', 'comment','pname'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = Land::find();

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);
        $dataProvider->setSort([
            'attributes' => [
                'name',
                'code',
                'addr',
                'scale',
                // 'map_image',
                'tel',
                'email',
                'pname' => [
                    'asc' => ['shbb_park.name' => SORT_ASC],
                    'desc' => ['shbb_park.name' => SORT_DESC],
                    'label' => '所属园区'
                ],
            ]
        ]);
        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        $query->andFilterWhere([
            'id' => $this->id,
            'park_id' => $this->park_id,
            'pricipal_id' => $this->pricipal_id,
        ]);

        $query->andFilterWhere(['like', 'code', $this->code])
            ->andFilterWhere(['like', 'name', $this->name])
            ->andFilterWhere(['like', 'introduction', $this->introduction])
            ->andFilterWhere(['like', 'scale', $this->scale])
            ->andFilterWhere(['like', 'map_image', $this->map_image])
            ->andFilterWhere(['like', 'tel', $this->tel])
            ->andFilterWhere(['like', 'email', $this->email])
            ->andFilterWhere(['like', 'comment', $this->comment])
            ->andFilterWhere(['like', 'shbb_park.name', $this->pname]);

        return $dataProvider;
    }
}
